{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简单使用示例 (线性回归)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0.026929826, 0.050091553]\n",
      "50 [0.10315169, 0.19829904]\n",
      "100 [0.10163783, 0.19911611]\n",
      "150 [0.10085113, 0.19954066]\n",
      "200 [0.1004423, 0.1997613]\n",
      "250 [0.10022987, 0.19987595]\n",
      "300 [0.10011947, 0.19993553]\n",
      "350 [0.10006209, 0.19996649]\n",
      "400 [0.10003228, 0.19998257]\n",
      "450 [0.10001679, 0.19999093]\n",
      "500 [0.10000876, 0.19999525]\n",
      "550 [0.10000459, 0.1999975]\n",
      "600 [0.10000243, 0.19999866]\n",
      "650 [0.10000128, 0.19999932]\n",
      "700 [0.10000069, 0.19999962]\n",
      "750 [0.10000043, 0.19999975]\n",
      "800 [0.10000043, 0.19999975]\n",
      "850 [0.10000043, 0.19999975]\n",
      "900 [0.10000043, 0.19999975]\n",
      "950 [0.10000043, 0.19999975]\n",
      "1000 [0.10000043, 0.19999975]\n",
      "1050 [0.10000043, 0.19999975]\n",
      "1100 [0.10000043, 0.19999975]\n",
      "1150 [0.10000043, 0.19999975]\n",
      "1200 [0.10000043, 0.19999975]\n",
      "1250 [0.10000043, 0.19999975]\n",
      "1300 [0.10000043, 0.19999975]\n",
      "1350 [0.10000043, 0.19999975]\n",
      "1400 [0.10000043, 0.19999975]\n",
      "1450 [0.10000043, 0.19999975]\n",
      "1500 [0.10000043, 0.19999975]\n",
      "1550 [0.10000043, 0.19999975]\n",
      "1600 [0.10000043, 0.19999975]\n",
      "1650 [0.10000043, 0.19999975]\n",
      "1700 [0.10000043, 0.19999975]\n",
      "1750 [0.10000043, 0.19999975]\n",
      "1800 [0.10000043, 0.19999975]\n",
      "1850 [0.10000043, 0.19999975]\n",
      "1900 [0.10000043, 0.19999975]\n",
      "1950 [0.10000043, 0.19999975]\n",
      "2000 [0.10000043, 0.19999975]\n"
     ]
    }
   ],
   "source": [
    "# 使用numpy生成100个随机数\n",
    "x_data=np.random.rand(100)\n",
    "y_data=x_data*0.1+0.2\n",
    "\n",
    "# 构造一个线性模型\n",
    "b=tf.Variable(0.0,)\n",
    "k=tf.Variable(0.0,)\n",
    "y=k*x_data+b\n",
    "\n",
    "# 二次代价函数\n",
    "loss=tf.reduce_mean(tf.square(y_data-y))\n",
    "# 梯度下降法训练的优化器\n",
    "optimizer=tf.train.GradientDescentOptimizer(0.1)\n",
    "# 最小化代价函数\n",
    "train=optimizer.minimize(loss)\n",
    "\n",
    "init =tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for step in range(2001):\n",
    "        sess.run(train)\n",
    "        if step%50==0:\n",
    "            print(step,sess.run([k,b]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
